# -*- coding: utf-8 -*-
import json
import time

from amac_spider.items import PofSubfundItem
from amac_spider.utils.my_redis_spider import RedisSpiderPro
from amac_spider.utils.handler import handle_url, get_url, get_date, get_datetime


# 证券公司私募投资基金
class PofSubfundSpider(RedisSpiderPro):
    name = 'pof_subfund'

    def parse(self, response):
        item = PofSubfundItem()
        mappings = {'product_id': 'productId', 'product_name': 'productName', 'product_code': 'productCode',
                    'manager': 'mgrName', 'found_time': 'foundDate', 'record_time': 'registeredDate',
                    'fund_type': 'fundType', 'organization_shape': 'orgForm', 'work_stat': 'fundStatus',
                    'trusteeship': 'tuoGuan', 'trustee': 'trustee', 'user_tenant_id': 'userTenantId'}
        content = json.loads(response.text)['content']
        for info in content:
            for k, v in mappings.items():
                item[k] = info[v]
            item['detail_url'] = handle_url(get_url(self.name), item['product_id'])
            item['found_time'] = get_date(item['found_time']/1000)
            item['record_time'] = get_date(item['record_time']/1000)
            item['crawl_time'] = get_datetime(time.time())
            yield item